﻿/*
 * Programer: JR Padfield
 * Description: Actual game play screen.
 * Version: 1
 * Date: 12/17/13 * 
 */

using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TheWorldLibrary.GameStates;
using TheWorld.Components;
using TheWorldLibrary.Worlds;
using TheWorldLibrary.TileEngine;
using TheWorldLibrary.Controls;
using TheWorld.Global;
using TheWorldLibrary.Input;
using Microsoft.Xna.Framework.Input;
using TheWorld.Networking;
using TheWorldLibrary.Sprites;
using TheWorldLibrary.Characters;
using TheWorldLibrary.Skills;

namespace TheWorld.GameScreens
{
    public class GamePlayScreen : BaseGameState
    {
        #region Field Region

        Engine engine = new Engine(32, 32);
        static Player player;
        static World world;

        public TextBox txtChat, txtRead;

        #endregion

        #region Property Region

        public static World World
        {
            get { return world; }
            set { world = value; }
        }

        public static Player Player
        {
            get { return player; }
            set { player = value; }
        }

        #endregion

        #region Constructor Region

        public GamePlayScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }

        #endregion

        #region XNA Method Region

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            LoadGui();
            base.LoadContent();

        }

        public override void Update(GameTime gameTime)
        {
            Mapping.Instance.Worlds.Update(gameTime);

            #region Handle Chat

            // Chat box handle enter
            if (txtChat.IsChat)
            {
                if (InputHandler.LastKeyboardState.IsKeyDown(Keys.Enter) && InputHandler.KeyboardState.IsKeyUp(Keys.Enter) && !txtChat.IsActive)
                {
                    txtChat.IsActive = true;
                }
                else if (txtChat.IsActive && InputHandler.LastKeyboardState.IsKeyDown(Keys.Enter) && InputHandler.KeyboardState.IsKeyUp(Keys.Enter))
                {
                    // Handle the text to be sent in message
                    if (txtChat.Text.Length > 0) // no text
                    {
                        ClientTcp.Instance.MapMsg(txtChat.Text);
                        txtChat.Text = "";
                        txtChat.IsActive = false;
                    }

                }
            }

            if (!txtChat.IsActive)
            {
                player.Update(gameTime);
            }

            // update txtbox positions
            txtRead.Position = new Vector2(player.Camera.Position.X + 10, player.Camera.Position.Y + 550);
            txtChat.Position = new Vector2(player.Camera.Position.X + 10, player.Camera.Position.Y + 710);

            #endregion

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin(
                SpriteSortMode.Deferred,
                BlendState.AlphaBlend,
                SamplerState.PointClamp,
                null,
                null,
                null,
                player.Camera.Transformation);

            

            Mapping.Instance.Worlds.DrawLevel(gameTime, GameRef.SpriteBatch, player.Camera);
            player.Draw(gameTime, GameRef.SpriteBatch);

            base.Draw(gameTime);

            GameRef.SpriteBatch.End();
        }

        #endregion

        #region Method Region

        private void LoadGui()
        {

            txtChat = new TextBox(GameRef, GfxManager.Instance.FONT_TEXTURE[3], GfxManager.Instance.Gui[13], GfxManager.Instance.Gui[13], GameRef.SpriteBatch);
            txtChat.Position = new Vector2(player.Camera.Position.X + 10, player.Camera.Position.Y + 710);
            txtChat.IsChat = true;
            Components.Add(txtChat);

            txtRead = new TextBox(GameRef, GfxManager.Instance.FONT_TEXTURE[3], GfxManager.Instance.Gui[4], GfxManager.Instance.Gui[13], GameRef.SpriteBatch);
            txtRead.Position = new Vector2(player.Camera.Position.X + 10, player.Camera.Position.Y + 550);
            txtRead.ReadOnly = true;
            Components.Add(txtRead);
        }

        public void CreatePlayer(string name, int spriteNum, EntityGender gender, Vector2 postion, int mapNum)
        {
            Dictionary<AnimationKey, Animation> animations = new Dictionary<AnimationKey, Animation>();

            Animation animation = new Animation(3, 32, 32, 0, 0);
            animations.Add(AnimationKey.Down, animation);

            animation = new Animation(3, 32, 32, 0, 32);
            animations.Add(AnimationKey.Left, animation);

            animation = new Animation(3, 32, 32, 0, 64);
            animations.Add(AnimationKey.Right, animation);

            animation = new Animation(3, 32, 32, 0, 96);
            animations.Add(AnimationKey.Up, animation);

            AnimatedSprite sprite = new AnimatedSprite(
                GfxManager.Instance.Sprites[spriteNum],
                animations);

            sprite.Position = postion;

            Entity entity = new Entity(
                name,
                Static.rSprite,
                DataManager.EntityData["Fighter"],
                gender,
                EntityType.Character);

            foreach (string s in DataManager.SkillData.Keys)
            {
                Skill skill = Skill.FromSkillData(DataManager.SkillData[s]);
                entity.Skills.Add(s, skill);
            }

            Character character = new Character(entity, sprite);

            Player = new Player(GameRef, character);

            Mapping.Instance.Worlds.CurrentLevel = mapNum;
        }

        public void UpdatePlayerXY(int x, int y)
        {
            player.Sprite.Position = new Vector2(x, y);
            player.Update(Static._gameTime);
        }

        #endregion
    }
}
